/* This class calls Add , Del or Mod class from processquery 
*package depending on 
 * the query type in calculation object passed to this class.
 * CalculationObject
			type 	Calculation
			Query 	insertQuery4	

*   InsertQuery4
	type 	add/mod/del
	 :		:
	 :		:
	 : 		:
 * 
 */


/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  event;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
import processquery.*;
import utility.CapitalChar;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class Calculation implements Event 
{
	String Expression[] ;
	String Result;
	String querytype;
	String Object;
	int PVOrder = 0;
	int PidOrder = 0;
	int MidOrder = 0;
	Map<Object, Object> mmap = new Hashtable<Object, Object>();
	
	Vector<Object> ExpressionVector = new Vector<Object>();
	GetIdName gin;
	String Request;
	//String MasterPath = "C://Reports//Basic_Reports//Master_Reports//";
	//String fileFormat = ".txt";
//	ReadFile rFile = new ReadFile();
	
	//public String tokens[]=new String[4];
	String tableName="master_table";
	String TYPE="type";
	String trans_type;
	String transaction_type = "transaction_type";
	String tid;
	String QUERY="query";
	

	
	public void process()
	{
		Hashtable<Object, Object> Condition = new Hashtable<Object, Object>();
		Condition.put("mid", Object);
		Condition.put("td", "null");
		Condition.put("pid", QUERY);
		String object1=Object;
		RecordSearch rs =new RecordSearch();
		rs.setConditionMap(Condition);
		String result[] = rs.getArrayFromResultSet();
		String expression_id = gin.getId(Object);
		for(int i=0;i<result.length;i++)                   //This for loop parses Calculationobject.  
		{												   	
			String [] Values = result[i].split("\t");
			try
			{
					PVOrder = Column.pv_index; 
					expression_id = Values[PVOrder-1];
					Hashtable<Object, Object> Condition1 = new Hashtable<Object, Object>();
					Condition1.put("mid", expression_id);
					Condition1.put("td", "null");
					Condition1.put("pid", TYPE);
					RecordSearch rs1 =new RecordSearch();
					rs1.setConditionMap(Condition1);
					String result1[] = rs1.getArrayFromResultSet();
					String Object1=expression_id;
					for(int j=0;j<result1.length;j++)   //This for loop parses InsertQuery4 object where we
					{									//can get Add/Mod/Del as type of query.
						Values = result1[j].split("\t");
						try
						{
							PVOrder = Column.pv_index; 
							expression_id = Values[PVOrder-1];							
							Query query;
							Class C = Class.forName("processquery."+CapitalChar.makeFirstCharCapital(gin.getItem(expression_id)));
							query = (Query)C.newInstance(); 
							query.setdatbase();
							query.setid(tid);
							query.setObject(Object1);
							query.processQuery();
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
					}//for
				//}//if Query2
			}//try outer
			catch (Exception e) 
			{
				e.printStackTrace();
				result[0]= "error";
			}
		
		}//for
	}//fn.
	
	
	/*public void process()
	{
		Hashtable<Object, Object> Condition = new Hashtable<Object, Object>();
		Condition.put("mid", Object);
		Condition.put("td", "null");
		Condition.put("pid", TYPE);
		RecordSearch rs =new RecordSearch(dbSQL);
		rs.setConditionMap(Condition);
		String result[] = rs.getArrayFromResultSet();
		String expression_id = Object;
		for(int i=0;i<result.length;i++)
		{
			String [] Values = result[i].split("\t");
			try
			{
				PVOrder = Column.pv_index;
				expression_id = Values[PVOrder-1];
				if(TYPE.equals("9") && expression_id.equals(gin.getId("Add")))
				{
						processquery.Add add=new processquery.Add();
						add.setdatbase(dbSQL);
						add.setid(tid);
						add.setObject(Object);
						add.processQuery();
						System.exit(0);
				}
				if(TYPE.equals("9") && expression_id.equalsIgnoreCase("Del"))
				{
						processquery.Del del = new processquery.Del();
						del.setdatbase(dbSQL);
						del.setid(tid);
						del.setObject(Object);
						del.processQuery();
						System.exit(0);
				}
				if(TYPE.equals("9") && expression_id.equalsIgnoreCase("Mod"))
				{
						processquery.Mod mod=new processquery.Mod();
						//mod.Init(Object);
						mod.setdatbase(dbSQL);
						mod.setid(tid);
						mod.setObject(Object);
						mod.processQuery();
						System.exit(0);
				}
				this.Object=expression_id;
				process();
			}//try
			catch(Exception e)
			{
					e.printStackTrace();
			}
		}//for
		
	}//fn.*/
	
	
	
	
	

	public void SetData(String Product, String type, String masterid,
			String userid, String Process,String datetime,String request) 
	{
	    	this.tid = masterid;
	}

	public boolean afterAdd() 
	{
		
		
		return false;
	}

	
	public boolean afterDel() 
	{
		
		return false;
	}

	public boolean afterMod() 
	{
		
		return false;
	}

	
	public boolean afterView() 
	{
		return false;
	}

	
	public void initializationData() 
	{
		gin = new GetIdName(); 
		TYPE = gin.getId(TYPE);
		QUERY =  gin.getId(QUERY);
	}

	public void setObject(String Object) {
		this.Object=Object;
		//this.querytype
		
		
	}
	public static void main(String[] args) 
	{
		Calculation cal = new Calculation();
		cal.initializationData();
		cal.setObject("2600046"); // This object is "calculationobject".
		cal.SetData("", "", "10","","","","");
		cal.process();

	}


}
